﻿using StaffingSystem.EF;
using StaffingSystem.Models;
using StaffingSystem.Tool;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using StaffingSystem.Business;

namespace StaffingSystem.Controllers
{
    public class TasksManagementController : Controller
    {
        #region 显示任务
       
        public ActionResult Tasks_Show()
        {
            return View();
        }
        /// <summary>
        /// 查看任务分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Tasks_Show(PageViewModel page)
        {
            string where = "";
            Pagelist pa = new Pagelist();
            if (page.Where != null)
            {

                PageCondition pc = new PageCondition();
                pc = JsonTool.JsonDeserialize<PageCondition>(page.Where);
                if (pc.SubAppstate == "已完成")
                {
                    where += "State=1";
                }
                else if (pc.SubAppstate == "未完成")
                {
                    where += "State=0";
                }
                else
                {
                    where += "(State=0 or State=1)";
                }
                if (pc.Name != null)
                {
                    where += " and (Originator like '%" + pc.Name + "%' or Participant like '%" + pc.Name + "%')";
                }
            }
            PageModel<View_Tasks> pagemodel = new PageModel<View_Tasks>();
            pagemodel.PageSize = page.PageSize;
            pagemodel.PageNumber = page.PageNumber;
            pagemodel.AscOrDesc = "";
            pagemodel.OrderID = "Id";
            pagemodel.PageKey = "Id";
            pagemodel.TableName = "View_Tasks";
            pagemodel.Where = where;
            pagemodel = pa.GetPageList(pagemodel);
            JsonResult result = new JsonResult();
            if (pagemodel.DataList.Count == 0)
            {
                result.Data = new
                {
                    State = 0,
                    Message = "没有记录",
                    Data = ""
                };
            }
            else
            {
                pagemodel.Where = "";
                result.Data = new
                {
                    State = 1,
                    Message = "获取数据成功",
                    Data = JsonTool.JsonSerializer(pagemodel)
                };
            }

            return result;
        }
        #endregion
        #region 添加任务
      
        // GET: TasksManagement
        public ActionResult Tasks_Add()
        {
            return View();
        }
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="ta"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Tasks_Add(View_Tasks ta)
        {
            JsonResult result = new JsonResult();
            Management db = new Management();
            StringBuilder procString = new StringBuilder();
            procString.Append("Proc_taskAdd");
            procString.Append(" @TaskName,");
            procString.Append("@BeginTime,");
            procString.Append("@PendTime,");
            procString.Append("@Originator,");
            procString.Append("@Participant,");
            procString.Append("@Remark,");
            procString.Append("@taskType");
            SqlParameter[] parameters = {
                    new SqlParameter("@TaskName",ta.TaskName),
                    new SqlParameter("@BeginTime",ta.BeginTime),
                    new SqlParameter("@PendTime",ta.PendTime),
                    new SqlParameter("@Originator","张三"),//发布人登陆人
                    new SqlParameter("@Participant",ta.Participant),
                    new SqlParameter("@Remark",ta.Remark),
                    new SqlParameter("@taskType",ta.TaskTypeld),
                };
            int a = db.Database.ExecuteSqlCommand(procString.ToString(), parameters);
            if (a > 0)
            {
                result.Data = new
                {
                    State = 1,
                    Message = "添加成功"
                };
            }
            else
            {
                result.Data = new
                {
                    State = 0,
                    Message = "添加失败"
                };
            }
            return result;
        }
        #endregion
        #region 下拉框
        /// <summary>
        /// 加载添加页面任务类型下拉框
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Select()
        {
            Management db = new Management();
            List<TaskType> tt = new List<TaskType>();
            tt = db.TaskType.ToList();
            JsonResult result = new JsonResult();
            result.Data = new
            {
                data = JsonTool.JsonSerializer(tt)
            };
            return result;
        }
        #endregion

        /// <summary>
        /// 任务详情显示页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Particulars(int id)
        {
            Management db = new Management();
            View_Tasks td = db.View_Tasks.FirstOrDefault(a => a.Id == id);
            JsonResult result = new JsonResult();
            result.Data = new
            {
                State = 0,
                Message = "获取数据成功",
                Data = JsonTool.JsonSerializer(td)
            };
            return result;
        }
        #region 删除
    
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Removetask(int id)
        {
            Management db = new Management();
            Ttasks td = db.Ttasks.FirstOrDefault(a => a.Id == id);
            db.Ttasks.Remove(td);
            db.SaveChanges();
            JsonResult result = new JsonResult();
            result.Data = new
            {
                State = 0,
                Message = "删除成功"
            };
            return result;
        }
        #endregion
        #region 修改


        /// <summary>
        /// 修改页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            Management db = new Management();
            View_Tasks td = db.View_Tasks.FirstOrDefault(a => a.Id == id);
            return View("Edit", td);
        }
        [HttpPost]
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        public ActionResult Edit(Tasks ta)
        {
            JsonResult result = new JsonResult();
            Management db = new Management();
            StringBuilder procString = new StringBuilder();
            procString.Append("Proc_TaskUpdate");
            procString.Append(" @id,");
            procString.Append("@Participant,");
            procString.Append("@Remark,");
            procString.Append("@Result,");
            procString.Append("@Stat");

            SqlParameter[] parameters = {
                    new SqlParameter("@id",ta.Id),
                    new SqlParameter("@Participant",ta.Participant),
                    new SqlParameter("@Remark",ta.Remark),
                    new SqlParameter("@Result",ta.Result),
                    new SqlParameter("@Stat",true)
                };
            int a = db.Database.ExecuteSqlCommand(procString.ToString(), parameters);
            if (a > 0)
            {
                result.Data = new
                {
                    State = 1,
                    Message = "修改成功"
                };
            }
            else
            {
                result.Data = new
                {
                    State = 0,
                    Message = "修改失败"
                };
            }
            return result;
        }
        #endregion
    }
}